layui.use(['form','jquery','table','layer'],function() {
    var form = layui.form,
        $ = layui.jquery,
        table = layui.table,
        layer = layui.layer;

    var tabInstance;

    //渲染表格，请求所有区域数据
    tabInstance = table.render({
        elem: '#houseList',//id选择器
        url: '/broker-house/getHouse', //数据接口
        page: true, //开启分页
        limit: 5, //每页显示的条数，默认5
        limits: [5, 10, 15,], //每页显示条数选择框数据
        request: {
            pageName: 'current',
            limitName: 'size'
        },
        cols: [[ //表头
            {field: 'brokerHouseId', title: '房源ID', width: 160, sort: true, fixed: 'left', align: 'center'},
            {field: 'brokerHouseNumber', title: '房间号', width: 120, align: 'center'},
            {field: 'floor', title: '楼层', width: 120, align: 'center'},
            {field: 'area', title: '面积(m²)', width: 120, align: 'center'},
            {field: 'price', title: '价格(元)', width: 120, align: 'center'},
            {field: 'houseOwner', title: '房主', width: 120, align: 'center'},
            {field: 'telephone', title: '电话', width: 120, align: 'center'},
            {field: 'brokerBuildingId', title: '所属楼盘ID', width: 120, align: 'center',hide:true},
            {field: 'brokerBuildingName', title: '所属楼盘名称', width: 120, align: 'center'},
            {field: 'adderId', title: '添加者ID', width: 120, align: 'center',hide:true},
            {field: 'userName', title: '添加者姓名', width: 120, align: 'center'},
            {field: 'state', title: '房屋状态', width: 120, align: 'center',templet:function (house) {
                console.log(house)
                switch (house.state){
                        case 1:
                            return "可租";
                            break;
                        case 2:
                            return "已租";
                            break;
                        case 3:
                            return "可售";
                            break;
                        case 4:
                            return "已定";
                            break;
                        case 5:
                            return "已售";
                            break;
                    }
                }},
            {field: 'operation', title: '操作', width: 180, fixed: 'right', toolbar: "#houseTool", align: 'center'}
        ]]
});
    //改删操作
    table.on('tool(houseList)',function(obj){
        switch (obj.event){
            case 'edit_btn':
                layer.open({
                    type: 2,
                    content: 'edit.jsp?id=' + obj.data.brokerBuildingId,
                    skin: 'layui-layer-molv',
                    title: "修改区域",
                    area: ["450px", "500px"],
                    btn: ['修改', '关闭'],
                    //弹出层的初始化操作
                    success: function (layero, index) {
                        //获取编辑表单
                        var edit = layer.getChildFrame('body', index).find("#edit");
                        //获取元素值
                        var brokerHouseNumber = edit.find("#brokerHouseNumber").val(obj.data.brokerHouseNumber);
                        var floor = edit.find("#floor").val(obj.data.floor);
                        var area = edit.find("#area").val(obj.data.area);
                        var price = edit.find("#price").val(obj.data.price);
                        var houseOwner = edit.find("#houseOwner").val(obj.data.houseOwner);
                        var telephone = edit.find("#telephone").val(obj.data.telephone);
                        var brokerBuildingId = edit.find("#brokerBuildingId").val(obj.data.brokerBuildingId);
                        var adderId = edit.find("#adderId").val(obj.data.adderId);
                        var state = edit.find("#state").val(obj.data.state);
                    },
                    yes:function (index, layero) {
                        //获取编辑表单
                        var edit = layer.getChildFrame('body', index).find("#edit");
                        //把数组转为JSON,请求后台执行添加操作，后台返回数据后显示
                        var data = {"brokerHouseId":obj.data.brokerHouseId};
                        edit.serializeArray().forEach(function(val){
                            data[val.name] = val.value;
                            console.log(val.name);
                            console.log(val.value);
                        });
                        $.post('/broker-house/update',data,function(info){
                            layer.close(index);//关闭窗口
                            layer.msg(info.msg);//弹出增加失败或者成功的信息
                            tabInstance.reload();//重新加载表
                        });

                    }
                });
                break;
            case 'del_btn':
                layer.confirm('你确定要删除房源【'+obj.data.brokerHouseNumber+'】吗？',{icon:3,anim: 4,skin:'layui-layer-molv'},function(){
                    var url = '/broker-house/deleteById/'+obj.data.brokerHouseId;
                    $.post(url,function(data){
                        console.log(data);
                        tabInstance.reload();//重新加载数据表格
                        layer.msg(data.msg);
                    });
                });
        }
    });
    //添加
        $(".add_btn").click(function(){
        layer.open({
            type: 2,
            content: 'add.jsp',
            skin:'layui-layer-molv',
            title:"添加楼盘",
            area:["450px","600px"],
            btn:['确定','关闭'],
            yes: function(index, layero){
                //获取编辑表单
                var add = layer.getChildFrame('body', index).find("#add");
                //把数组转为JSON,请求后台执行添加操作，后台返回数据后显示
                let data = {};
                add.serializeArray().forEach(function(val){
                    data[val.name] = val.value;
                    console.log(val.name);
                    console.log(val.value);
                });

                $.post('/broker-house/add',data,function(info){
                    layer.close(index);//关闭窗口
                    layer.msg(info.msg);//弹出增加失败或者成功的信息
                    tabInstance.reload();//重新加载表
                });
            }
        });
    });

        //查找
    $(".find_btn").click(function(){
        layer.open({
                type: 2,
                content: 'find.jsp',
                skin: 'layui-layer-molv',
                title: "用户管理",
                area: ["430px", "500px"],
                btn: ['确定', '关闭'],
                yes: function(index, layero){
                    let find_form = layer.getChildFrame('body', index).find("#find");
                    var price = find_form.find("#price").val();
                    var id = find_form.find("#area").val();
                    var state = find_form.find("#state").val();

                    //把数组转为JSON,请求后台执行添加操作，后台返回数据后显示
                    let data = {"id":id,"price":price,"state":state};
                    layer.close(index);//关闭窗口
                    table.render({
                        elem: '#houseList',//id选择器
                        url: '/broker-house/find', //数据接口
                        where:data,
                        page: true, //开启分页
                        limit:5, //每页显示的条数，默认5
                        limits:[5,10,15,], //每页显示条数选择框数据
                        request:{
                            pageName: 'current',
                            limitName: 'size'
                        },
                        cols: [[ //表头
                            {field: 'brokerHouseId', title: '房源ID', width: 160, sort: true, fixed: 'left', align: 'center'},
                            {field: 'brokerHouseNumber', title: '房间号', width: 120, align: 'center'},
                            {field: 'floor', title: '楼层', width: 120, align: 'center'},
                            {field: 'area', title: '面积(m²)', width: 120, align: 'center'},
                            {field: 'price', title: '价格(元)', width: 120, align: 'center'},
                            {field: 'houseOwner', title: '房主', width: 120, align: 'center'},
                            {field: 'telephone', title: '电话', width: 120, align: 'center'},
                            {field: 'brokerBuildingId', title: '所属楼盘ID', width: 120, align: 'center'},
                            {field: 'brokerBuildingName', title: '所属楼盘名称', width: 120, align: 'center'},
                            {field: 'adderId', title: '添加者ID', width: 120, align: 'center'},
                            {field: 'userName', title: '添加者姓名', width: 120, align: 'center'},
                            {field: 'state', title: '房屋状态', width: 120, align: 'center'},
                            {field: 'operation', title: '操作', width: 180, fixed: 'right', toolbar: "#houseTool", align: 'center'}
                        ]]
                    });
                }
            }
        )
    });
});